//
//  ViewController.m
//  3 -- QQ project
//
//  Created by Rainy Zou on 15/11/24.
//  Copyright © 2015年 Rainy Zou. All rights reserved.
//

#import "ViewController.h"
#import "MessageFrame.h"
#import "JSMessage.h"
#import "JSMessageCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong) NSMutableArray *messageFrames;

@property (nonatomic,weak) IBOutlet UITableView *tableView;

@property (nonatomic,weak) IBOutlet UITextField *textField;

@end


@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    _textField.delegate = self;
    
    self.messageFrames = [NSMutableArray array];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"messages" ofType:@"plist"];
    
    NSArray *arrays = [NSArray arrayWithContentsOfFile:path];
    JSMessage *lastMessage = [[JSMessage alloc]init];
    for (NSDictionary *dict in arrays) {
        MessageFrame *messageFrame = [[MessageFrame alloc]init];
        JSMessage *message = [JSMessage messageWithDict:dict];
        if (lastMessage.time == message.time) {
            message.hiddenTime = TRUE;
        }
        messageFrame.message = message;
        
        lastMessage = message;
        
        [_messageFrames addObject:messageFrame];
    }
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.allowsSelection = NO;
    _tableView.backgroundColor = [[UIColor alloc]initWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardChange:(NSNotification *)noti
{
    NSLog(@"%@",noti.userInfo);
    
    CGFloat during = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    CGRect endFrame = [noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    CGFloat offset = endFrame.origin.y - self.view.frame.size.height;
    [UIView animateWithDuration:during animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, offset);
    }];
    
//    UIKeyboardAnimationCurveUserInfoKey = 7;
//    UIKeyboardAnimationDurationUserInfoKey = "0.25";
//    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
//    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
//    UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
//    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
//    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
//    UIKeyboardIsLocalUserInfoKey = 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _messageFrames.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *resuseID = @"cell";
    JSMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:resuseID];
    if(cell == nil){
        cell = [[JSMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resuseID];
    }
    
    cell.messageFrame = _messageFrames[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageFrame *messageFrame = _messageFrames[indexPath.row];
    return messageFrame.rowHeight;
}

#pragma mark 退出键盘
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //退出键盘
    [self.view endEditing:YES];
}

//#pragma mark 文本框提交
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"进入TextField");
    [self sendMessage:_textField.text messageType:JSMessageTypeMe];
    
    [self sendMessage:@"gun" messageType:JSMessageTypeOther];
    
    [self.tableView reloadData];
    NSIndexPath *path = [NSIndexPath indexPathForRow:_messageFrames.count - 1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    return YES;
}

- (void)sendMessage:(NSString *)msgText messageType:(JSMessageType)type
{
    JSMessage *message = [[JSMessage alloc]init];
    message.text = msgText;
    message.type = type;
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"HH:mm";
    
    message.time = [formatter stringFromDate:date];
    
    MessageFrame *lastFrame = [_messageFrames lastObject] ;
    
    if([lastFrame.message.time isEqualToString:message.time])
    {
        message.hiddenTime = TRUE;
    }
    MessageFrame *frame = [[MessageFrame alloc]init];
    frame.message = message;
    [_messageFrames addObject:frame];
    
    
}

@end
